package com.example.hp.myapplication.fragment;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


import com.example.hp.myapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by M on 2018/5/11.
 */

public class Fragment2 extends Fragment {
    public Fragment2() {
        super();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.frag_dload1,container,false);
        initListView(view);
        return view;
    }

    //加载listview
    private void initListView(View view){
        ListView listView = (ListView) view.findViewById(R.id.lvMenu);
        SimpleAdapter simpleAda;
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        final String[] arrName = new String[]{"菜单1","菜单2","自定义对话框","扫一扫"};
        int[] arrImg= new int[]{R.drawable.icon1,R.drawable.icon2,R.drawable.icon3,R.drawable.icon4};

        //创建list
        for(int i=0;i<arrName.length;i++){
            Map<String,Object> record = new HashMap<String, Object>();
            record.put("img", arrImg[i]);
            record.put("txt", arrName[i]);
            list.add(record);
        }

        simpleAda = new SimpleAdapter(getActivity(), list, R.layout.frag_dload1_listitem,
                new String[]{"img","txt"},
                new int[]{R.id.ivPic,R.id.tvMenu});
        //将数据加载到list中
        listView.setAdapter(simpleAda);
        //设置listview的单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "你点击的是"+arrName[position], Toast.LENGTH_SHORT).show();
                if(position == 3){
                    Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    getActivity().startActivityForResult(it,100);
                }
            }
        });
    }

}
